using System;
using System.Collections.Generic;
using System.Data;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentManagement.MetaData.Builders;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Projections.Models;
using Orchard.Data;
using Orchard.Localization;
using Contrib.BSCarouselSlide.Models;

namespace Contrib.BSCarouselSlide {
    public class Migrations : DataMigrationImpl {
        private readonly IRepository<MemberBindingRecord> _memberBindingRepository;

        public Migrations(IRepository<MemberBindingRecord> memberBindingRepository)
        {
            _memberBindingRepository = memberBindingRepository;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        public int Create() {
            // Create the BSCarouselSlidePart table
            SchemaBuilder.CreateTable("BSCarouselSlidePartRecord", table => table
                .ContentPartRecord()
                .Column<string>("CarouselKey", c => c.WithLength(60))
                .Column<int>("Priority")
                .Column<bool>("HasText",c => c.WithDefault(true))
                .Column<string>("TextClass", c => c.WithLength(60))
                .Column<bool>("HasImage", c => c.WithDefault(true))
                .Column<string>("ImageClass", c => c.WithLength(60))
                );
            ContentDefinitionManager.AlterPartDefinition("BSCarouselSlidePart", part => part
                    //.WithField("Image", field => field.OfType("MediaPickerField")
                    .WithField("Image", field => field.OfType("MediaLibraryPickerField")                        
                    .WithDisplayName("Image")
                    .WithSetting("Hint", "Select Image for Button")));
            // Define the BSCarouselSlide Part
            ContentDefinitionManager.AlterPartDefinition("BSCarouselSlidePart", part => part.Attachable(true));
            // Define the BSCarouselSlide Type similar to Page with a mediapicker field and an index
            ContentDefinitionManager.AlterTypeDefinition("BSCarouselSlide",
                            cfg => cfg
                            .WithPart("BSCarouselSlidePart")
                            .WithPart("CommonPart", p => p
                                .WithSetting("DateEditorSettings.ShowDateEditor", "true"))
                            .WithPart("PublishLaterPart")
                            .WithPart("TitlePart")
                            .WithPart("AutoroutePart", builder => builder
                                .WithSetting("AutorouteSettings.AllowCustomPattern", "true")
                                .WithSetting("AutorouteSettings.AutomaticAdjustmentOnEdit", "false")
                                .WithSetting("AutorouteSettings.PatternDefinitions", "[{Name:'Title', Pattern: '{Content.Slug}', Description: 'my-slide'}]")
                                .WithSetting("AutorouteSettings.DefaultPatternIndex", "0"))
                            .WithPart("BodyPart")             
                            .Draftable(true)
                            .Creatable());
            
            // Binding for Priority in order to use it in queries
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "Priority",
                DisplayName = "Slide priority",
                Description = "Order for Carousel"
            });
            // Binding for Priority in order to use it in queries
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "CarouselKey",
                DisplayName = "Carousel Key",
                Description = "Key to filter various carousels"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "TextClass",
                DisplayName = "TextClass",
                Description = "Css class name for text block animation"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "ImageClass",
                DisplayName = "ImageClass",
                Description = "Css class name for image animation"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "HasText",
                DisplayName = "HasText",
                Description = "This slide has a Body Part displayed"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "HasImage",
                DisplayName = "HasImage",
                Description = "This slide has an ImagePicker Field displayed"
            });
            return 2;
        }

        public int UpdateFrom1()
        {

            SchemaBuilder.AlterTable("BSCarouselSlidePartRecord", table => table.AddColumn<bool>("HasText",c => c.WithDefault(true)));
            SchemaBuilder.AlterTable("BSCarouselSlidePartRecord", table => table.AddColumn<bool>("HasImage", c => c.WithDefault(true)));
            
            // Binding for Priority in order to use it in queries
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "CarouselKey",
                DisplayName = "Carousel Key",
                Description = "Key to filter various carousels"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "TextClass",
                DisplayName = "TextClass",
                Description = "Css calass name for text block animation"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "ImageClass",
                DisplayName = "ImageClass",
                Description = "Css calass name for image animation"
            });

            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "HasText",
                DisplayName = "HasText",
                Description = "This slide has a Body Part displayed"
            });
            _memberBindingRepository.Create(new MemberBindingRecord
            {
                Type = typeof(BSCarouselSlidePartRecord).FullName,
                Member = "HasImage",
                DisplayName = "HasImage",
                Description = "This slide has an ImagePicker Field displayed"
            });
            return 2;
        }
                
    }
}